package gemini.basic.manager.impl;

import gemini.basic.dao.CategoryDao;
import gemini.basic.dao.ProductDao;
import gemini.basic.dao.ProductDetailDao;
import gemini.basic.dto.SearchProductCriteria;
import gemini.basic.manager.ProductManager;
import gemini.basic.model.Product;
import gemini.basic.model.ProductDetail;
import gemini.basic.model.ProductType;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProductManagerImpl implements ProductManager {
	
	private final Logger logger = LoggerFactory.getLogger(ProductManager.class);
	
	private ProductDao productDao;

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	
	private ProductDetailDao productDetailDao;

	public void setProductDetailDao(ProductDetailDao productDetailDao) {
		this.productDetailDao = productDetailDao;
	}
	
	private CategoryDao categoryDao;

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	
	@Override
	public Product getProductByCode(String code){
		
		logger.debug("============== get product from db by code");

		Product result = productDao.getByCode(code);
		return result;
	}

	@Override
	public List<ProductType> getAllCategory() {
		logger.debug("============== get all category");
		List<ProductType> result = categoryDao.getAll();
		return result;
	}
	
	@Override
	public ProductType getCategoryByCode(String code) {
		logger.debug("============== get category by code");
		ProductType result = categoryDao.getByCode(code);
		return result;
	}
	
	@Override
	public List<Product> findProduct(
			SearchProductCriteria criteria) {
		List<Product> result = productDao.findProduct(criteria);
		return result;
	}

	@Override
	public void deleteProductType(Integer id) {
		categoryDao.delete(categoryDao.getById(id));
	}

	@Override
	public ProductType saveOrUpdateProductType(ProductType productType) {
		if (productType.isPersisted()) {
			ProductType dbProductType = categoryDao.getById(productType.getId());
			
			dbProductType.setName(productType.getName());
			dbProductType.setDescription(productType.getDescription());
			dbProductType.setCode(productType.getCode());
			
			productType = dbProductType;
		}

		ProductType result = categoryDao.saveOrUpdate(productType, true);

		return result;
	}
	
	@Override
	public List<Product> getAllProducts() {
		List<Product> result = productDao.getAll();
		return result;
	}
	
	@Override
	public void deleteProduct(Integer id) {
		productDao.delete(productDao.getById(id));
	}

	@Override
	public Product saveOrUpdateProduct(Product product) {
		if (product.isPersisted()) {
			Product dbProduct = productDao.getById(product.getId());
			
			dbProduct.setName(product.getName());
			dbProduct.setCode(product.getCode());
			dbProduct.setImageUrl(product.getImageUrl());
			
			dbProduct.getProductDetail().setContent(product.getProductDetail().getContent());
			dbProduct.getProductDetail().setFastFacts(product.getProductDetail().getFastFacts());
			dbProduct.getProductDetail().setKeyBenefit(product.getProductDetail().getKeyBenefit());
			dbProduct.getProductDetail().setOverview(product.getProductDetail().getOverview());
			dbProduct.getProductDetail().setUserGuide(product.getProductDetail().getUserGuide());
			
			product = dbProduct;
		} else {
			ProductDetail productDetail = productDetailDao.saveOrUpdate(product.getProductDetail(), true);
			product.setProductDetail(productDetail);
		}
		
		ProductType prType = categoryDao.getById(product.getProductType().getId());
		product.setProductType(prType);

		Product result = productDao.saveOrUpdate(product, true);

		return result;
	}
}
